// import '@components/global.less'
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import store from './store/index'
import 'element-ui/lib/theme-chalk/index.css'
// import { initMenu } from './utils/menus'
import VueRouter from 'vue-router'

// 以下代码是为了解决vue-router的内部错误，没有进行catch处理，导致的编程式导航跳转问题，往同一地址跳转会报错的情况
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call (this, location).catch(err => err)
}

import { postRequest } from './utils/api'
import { getRequest } from './utils/api'
import { putRequest } from './utils/api'
import { deleteRequest } from './utils/api'

Vue.config.productionTip = false
Vue.use(ElementUI)

// 插件形式使用请求
Vue.prototype.postRequest = postRequest
Vue.prototype.getRequest = getRequest
Vue.prototype.putRequest = putRequest
Vue.prototype.deleteRequest = deleteRequest

router.beforeEach((to, from, next) => {
  // console.log(window.sessionStorage.getItem('tokenStr'))
  // console.log(window.sessionStorage.getItem('user'))
  // console.log(window.sessionStorage.getItem('psd'))
  if (window.sessionStorage.getItem('tokenStr')) {
    // initMenu(router, store)
    // if (!window.sessionStorage.getItem('user')) {
    //   // 判断用户信息是否存在
    //   return getRequest ('/admin/info').then(res => {
    //     if (res) {
    //       // 存入用户信息
    //       window.sessionStorage.setItem('user', JSON.stringify(res))
    //       next()
    //     }
    //   })
    // }
    next()
  } else {
    if (to.path == '/') {
      next()
    } else {
      next('/?redirect=' + to.path)
    }
  }
  next()
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
